package cn.iocoder.yudao.module.moa.controller.admin.qjapply;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.moa.controller.admin.qjapply.vo.*;
import cn.iocoder.yudao.module.moa.dal.dataobject.qjapply.QjApplyDO;
import cn.iocoder.yudao.module.moa.service.qjapply.QjApplyService;

/**
 * 请假申请
 */
@Tag(name = "管理后台 - MOA 请假申请")
@RestController
@RequestMapping("/moa/qj-apply")
@Validated
public class QjApplyController {

    @Resource
    private QjApplyService qjApplyService;

    @PostMapping("/create")
    @Operation(summary = "创建MOA 请假申请")
    public CommonResult<Long> createQjApply(@Valid @RequestBody QjApplySaveReqVO createReqVO) {
        return success(qjApplyService.createQjApply(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新MOA 请假申请")
    public CommonResult<Boolean> updateQjApply(@Valid @RequestBody QjApplySaveReqVO updateReqVO) {
        qjApplyService.updateQjApply(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除MOA 请假申请")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteQjApply(@RequestParam("id") Long id) {
        qjApplyService.deleteQjApply(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得MOA 请假申请")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<QjApplyRespVO> getQjApply(@RequestParam("id") Long id) {
        QjApplyDO qjApply = qjApplyService.getQjApply(id);
        return success(BeanUtils.toBean(qjApply, QjApplyRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得MOA 请假申请分页")
    public CommonResult<PageResult<QjApplyRespVO>> getQjApplyPage(@Valid QjApplyPageReqVO pageReqVO) {
        PageResult<QjApplyDO> pageResult = qjApplyService.getQjApplyPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, QjApplyRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出MOA 请假申请 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportQjApplyExcel(@Valid QjApplyPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<QjApplyDO> list = qjApplyService.getQjApplyPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "MOA 请假申请.xls", "数据", QjApplyRespVO.class,
                        BeanUtils.toBean(list, QjApplyRespVO.class));
    }

}
